﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.XPath;

namespace ZYO.Shared.DataHelper
{
    class XQueryHelper
    {
        XPathNavigator nav;
        XPathDocument docNav;
        XPathNodeIterator NodeIter;
        String strExpression;

        public XQueryHelper()
        {


        }

        public void Query()
        {

            docNav = new XPathDocument(@"c:\books.xml");
            // Create a navigator to query with XPath.
            nav = docNav.CreateNavigator();
            // Find the average cost of a book.
            // This expression uses standard XPath syntax.
            strExpression = "sum(/bookstore/book/price) div count(/bookstore/book/price)";
            // Use the Evaluate method to return the evaluated expression.
            Console.WriteLine("The average cost of the books are {0}", nav.Evaluate(strExpression));

            // Find the title of the books that are greater then $10.00.
            strExpression = "/bookstore/book/title[../price>10.00]";


            // Select the node and place the results in an iterator.
            NodeIter = nav.Select(strExpression);

            Console.WriteLine("List of expensive books:");
            //Iterate through the results showing the element value.
            while (NodeIter.MoveNext())
            {
                Console.WriteLine("Book Title: {0}", NodeIter.Current.Value);
            };

            // Pause
            Console.ReadLine();




        }
			
    }
}
